package com.cyj.spring.springframeword.beans.factory.support;

import com.cyj.spring.springframeword.beans.BeansException;
import com.cyj.spring.springframeword.beans.factory.config.BeanDefinition;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 默认列表Bean工厂
 * @BelongsProject: small-spring
 * @BelongsPackage: com.cyj.spring.springframeword.beans.factory.support
 * @Author: ChenYongJia
 * @CreateTime: 2021-07-26 14:39
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry {

    private Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>(16);

    /**
     * 注册的 Bean 定义
     * 
     * @param beanName bean名称
     * @param beanDefinition bean定义
     * @return void
     * @date 2021/7/27 15:30
     * @author ChenYongJia
     * @version 1.0
     */
    @Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
        beanDefinitionMap.put(beanName, beanDefinition);
    }

    @Override
    public BeanDefinition getBeanDefinition(String beanName) throws BeansException {
        BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
        if (beanDefinition == null) throw new BeansException("No bean named '" + beanName + "' is defined");
        return beanDefinition;
    }

}
